﻿// CWindowsHookDlg.cpp: 实现文件
//

#include "pch.h"
#include "HackSkill.h"
#include "CWindowsHookDlg.h"
#include "afxdialogex.h"

#ifdef _WIN64
#pragma comment(lib,"../x64/Debug\\InjectByWindowsHook.lib")
#else
#pragma comment(lib,"../Debug\\InjectByWindowsHook.lib")
#endif


extern "C" __declspec(dllimport) void SetGlobalHook(int dwHookCode);
extern "C" __declspec(dllimport) void UninstallGlobalHook();
extern "C" __declspec(dllimport) wchar_t pTargetProcessName[MAX_PATH];



// CWindowsHookDlg 对话框

IMPLEMENT_DYNAMIC(CWindowsHookDlg, CDialogEx)

CWindowsHookDlg::CWindowsHookDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_WindowsHookDlg, pParent)
{

}

CWindowsHookDlg::~CWindowsHookDlg()
{
}

void CWindowsHookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//  DDX_Text(pDX, IDC_EDIT1, csTargetProcessName);
	DDX_Control(pDX, IDC_COMBO1, m_HookType);
}


BEGIN_MESSAGE_MAP(CWindowsHookDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_SetHook, &CWindowsHookDlg::OnBnClickedButtonSethook)
	ON_BN_CLICKED(IDC_BUTTON_UnHook, &CWindowsHookDlg::OnBnClickedButtonUnhook)
END_MESSAGE_MAP()


// CWindowsHookDlg 消息处理程序


BOOL CWindowsHookDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_HookType.AddString(L"WH_CALLWNDPROC");
	m_HookType.AddString(L"WH_CALLWNDPROCRET");
	m_HookType.AddString(L"WH_CBT");
	m_HookType.AddString(L"WH_DEBUG");
	m_HookType.AddString(L"WH_FOREGROUNDIDLE");
	m_HookType.AddString(L"WH_GETMESSAGE");
	m_HookType.AddString(L"WH_JOURNALPLAYBACK");
	m_HookType.AddString(L"WH_JOURNALRECORD");
	m_HookType.AddString(L"WH_KEYBOARD");
	m_HookType.AddString(L"WH_KEYBOARD_LL");
	m_HookType.AddString(L"WH_MOUSE");
	m_HookType.AddString(L"WH_MOUSE_LL");
	m_HookType.AddString(L"WH_MSGFILTER");
	m_HookType.AddString(L"WH_SHELL");
	m_HookType.AddString(L"WH_SYSMSGFILTER");

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CWindowsHookDlg::OnBnClickedButtonSethook()
{
	// TODO: 在此添加控件通知处理程序代码
	//更新数据到变量
	UpdateData(TRUE);


	//获取Hook类型
	CString csHookCode;
	m_HookType.GetWindowTextW(csHookCode);
	int nHookCode=-10;
	if (csHookCode == L"")
	{
		MessageBox(L"选择钩子类型！",L"提示");
		return;
	}
	else if (csHookCode == L"WH_CALLWNDPROC")
	{
		nHookCode = WH_CALLWNDPROC;
	}
	else if (csHookCode == L"WH_CALLWNDPROCRET")
	{
		nHookCode = WH_CALLWNDPROCRET;
	}
	else if (csHookCode == L"WH_CBT")
	{
		nHookCode = WH_CBT;
	}
	else if (csHookCode == L"WH_DEBUG")
	{
		nHookCode = WH_DEBUG;
	}
	else if (csHookCode == L"WH_FOREGROUNDIDLE")
	{
		nHookCode = WH_FOREGROUNDIDLE;
	}
	else if (csHookCode == L"WH_GETMESSAGE")
	{
		nHookCode = WH_GETMESSAGE;
	}
	else if (csHookCode == L"WH_JOURNALPLAYBACK")
	{
		nHookCode = WH_JOURNALPLAYBACK;
	}
	else if (csHookCode == L"WH_JOURNALRECORD")
	{
		nHookCode = WH_JOURNALRECORD;
	}
	else if (csHookCode == L"WH_KEYBOARD")
	{
		nHookCode = WH_KEYBOARD;
	}
	else if (csHookCode == L"WH_KEYBOARD_LL")
	{
		nHookCode = WH_KEYBOARD_LL;
	}
	else if (csHookCode == L"WH_MOUSE")
	{
		nHookCode = WH_MOUSE;
	}
	else if (csHookCode == L"WH_MOUSE_LL")
	{
		nHookCode = WH_MOUSE_LL;
	}
	else if (csHookCode == L"WH_MSGFILTER")
	{
		nHookCode = WH_MSGFILTER;
	}
	else if (csHookCode == L"WH_SHELL")
	{
		nHookCode = WH_SHELL;
	}
	else if (csHookCode == L"WH_SYSMSGFILTER")
	{
		nHookCode = WH_SYSMSGFILTER;
	}
	SetGlobalHook(nHookCode);

}


void CWindowsHookDlg::OnBnClickedButtonUnhook()
{
	// TODO: 在此添加控件通知处理程序代码
	UninstallGlobalHook();
}
